package com.funny.learning.english.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.funny.learning.english.BaseActivity;
import com.funny.learning.english.R;
import com.funny.learning.english.api.BaseAjaxCallBack.IAjaxCallbackMMK;
import com.funny.learning.english.api.TiengAnhApi;
import com.funny.learning.english.model.UserEntity;
import com.funny.learning.english.util.KeyHashUtil;
import com.funny.learning.english.util.PreferenceShUtil;
import com.sromku.simple.fb.Permission.Type;
import com.sromku.simple.fb.listeners.OnLoginListener;

public class LoginActivity extends BaseActivity implements OnClickListener {
	private TextView tvRegister, tvForgotPass;
	private Button btSignIn;
	private EditText edtUser;
	private EditText edtPass;
	private Button btSignInFace;
	private Button btSignInGoogle;
	private CheckBox cbSave;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_layout);
		KeyHashUtil.getAppKeyHash(this);
		tvRegister = (TextView) findViewById(R.id.tvRegister);
		tvForgotPass = (TextView) findViewById(R.id.tvForgotPass);
		btSignIn = (Button) findViewById(R.id.btSignIn);
		edtUser = (EditText) findViewById(R.id.edtUser);
		edtPass = (EditText) findViewById(R.id.edtPass);
		btSignInFace = (Button) findViewById(R.id.btSignInFace);
		btSignInGoogle = (Button) findViewById(R.id.btSignInGoogle);
		cbSave = (CheckBox) findViewById(R.id.cbSave);

		btSignIn.setOnClickListener(this);
		btSignInFace.setOnClickListener(this);
		btSignInGoogle.setOnClickListener(this);

		tvRegister.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				startActivity(new Intent(LoginActivity.this,
						RegisterActivity.class));
			}
		});

		tvForgotPass.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				startActivity(new Intent(LoginActivity.this,
						ForgotActivity.class));
			}
		});

		// Bundle bundle = getIntent().getExtras();
		// bundle.getSerializable(Define.KEY_USER);
	}

	@Override
	public void onClick(View paramView) {
		switch (paramView.getId()) {
		case R.id.btSignIn:
			String user = edtUser.getText().toString();
			String pass = edtPass.getText().toString();
			if (!user.isEmpty() && !pass.isEmpty()) {
				TiengAnhApi.login(user, pass, activity, callBackLogin);
			}
			break;
		case R.id.btSignInFace:
			onClickBtSignInFace();
			break;
		case R.id.btSignInGoogle:

			break;
		case R.id.tvRegister:
			startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
			break;
		case R.id.tvForgotPass:
			startActivity(new Intent(LoginActivity.this, ForgotActivity.class));
			break;
		default:
			break;
		}
	}

	private void onClickBtSignInFace() {
		String accessToken = getToken();
		if (!TextUtils.isEmpty(accessToken)) {
			TiengAnhApi.loginWithFacebook(accessToken, activity,
					loginWithFacebookAjaxCallbackMMK);

		} else {
			mSimpleFacebook.login(mOnLoginListener);
		}

	}

	private OnLoginListener mOnLoginListener = new OnLoginListener() {

		@Override
		public void onFail(String reason) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onException(Throwable throwable) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onThinking() {
			// TODO Auto-generated method stub

		}

		@Override
		public void onNotAcceptingPermissions(Type type) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onLogin() {
			// TODO Auto-generated method stub
			TiengAnhApi.loginWithFacebook(getToken(), activity,
					loginWithFacebookAjaxCallbackMMK);

		}
	};

	private IAjaxCallbackMMK loginWithFacebookAjaxCallbackMMK = new IAjaxCallbackMMK() {

		@Override
		public <T> void handler(T t, int code) {
			// TODO Auto-generated method stub
			onHandler(t, code);
		}
	};

	private IAjaxCallbackMMK callBackLogin = new IAjaxCallbackMMK() {

		@Override
		public <T> void handler(T t, int code) {

			onHandler(t, code);
		}

	};

	private <T> void onHandler(T t, int code) {
		if (code == IAjaxCallbackMMK.COMPLETE) {
			String userEntity = (String) t;
			
			try {
				JSONObject jsonObject = new JSONObject(userEntity);
				
				JSONObject jsonUserInfo = jsonObject.getJSONObject("result");
//				JSONObject jsonCoin = jsonObject.getJSONObject("coin");
				
				UserEntity entity = new UserEntity(jsonUserInfo, null);
				appication.setUserEntity(entity);

//				if (cbSave.isChecked()) {
					PreferenceShUtil.setValue(activity, PreferenceShUtil.KEY_ACC,
							userEntity.toString());
//				}else{
//					PreferenceShUtil.setValue(activity, PreferenceShUtil.KEY_ACC,
//							"");
//				}

				startActivity(new Intent(activity, OptionActivity.class));
				finish();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//			

		} else if (code == IAjaxCallbackMMK.ERROR) {
			Toast.makeText(activity, "Đăng nhập không thành công",
					Toast.LENGTH_SHORT).show();
		}
	}
}
